var MixlyEnv = {};

/**
  * 检测当前环境
  * @type {Boolean}，true - mixly Client; false - mixly Web
  */
MixlyEnv.isClient = true;

/**
  * 获取当前mixly2.0的路径
  * @type {String}
  */
MixlyEnv.clientPath = null;

/**
  * 检测当前系统
  * @type {String} win、darwin、linux
  */
MixlyEnv.currentPlatform = null;

/**
  * 对于win系统，获取免安装python3的路径，对于mac与linux，则此变量值为python3
  * @type {String} 
  */
MixlyEnv.python3Path = null;

/**
  * 获取mixly.py的路径
  * @type {String} 
  */
MixlyEnv.pyFilePath = null;

/**
  * 获取板卡index或主页面index的路径
  * @type {String} 
  */
MixlyEnv.indexPath = null;

/**
  * 获取板卡index或主页面index的缩放比例
  * @type {String} 
  */
MixlyEnv.winSize = null;

/**
  * 获取板卡index默认xml
  * @type {String} 
  */
MixlyEnv.defaultXML = null;

/**
  * 获取第三方库所用css
  * @type {Array} 
  */
MixlyEnv.thirdPartyCSS = [];

/**
  * 获取第三方库所用js
  * @type {Array} 
  */
MixlyEnv.thirdPartyJS = [];

try {
    MixlyEnv.isClient = (window && window.process && window.process.versions && window.process.versions['electron']) ? true : false;
} catch (error) {
    MixlyEnv.isClient = false;
}

const path = MixlyEnv.isClient && require('path');
const resolve = MixlyEnv.isClient && path.resolve;
const child_process = MixlyEnv.isClient && require("child_process");
const nodeFs = MixlyEnv.isClient && require('fs');
const electron = MixlyEnv.isClient && require('electron');
const blockly_clipboard = MixlyEnv.isClient && electron.clipboard;
const nodeOs = MixlyEnv.isClient && require('os');
const electronApp = MixlyEnv.isClient && require('@electron/remote').app;
const electronwebContents = MixlyEnv.isClient && require('@electron/remote').getCurrentWebContents();
const nowWindow = MixlyEnv.isClient && require('@electron/remote').getCurrentWindow();

if (MixlyEnv.isClient) {
    MixlyEnv.currentPlatform = nodeOs.platform();
    console.log(MixlyEnv.currentPlatform);
    if (MixlyEnv.currentPlatform == "darwin") {
        MixlyEnv.clientPath = resolve(electronApp.getPath("exe"), '../../../../');
    } else {
        MixlyEnv.clientPath = resolve(electronApp.getPath("exe"), '../');
    }
    try {
        MixlyEnv.clientPath = MixlyEnv.clientPath.replace(/\\/g, "/");
    } catch (e) {
        console.log(e);
    }
    MixlyEnv.pyFilePath = MixlyEnv.clientPath + '/mixpyBuild/mixly.py';
    if (MixlyEnv.currentPlatform == "darwin" || MixlyEnv.currentPlatform == "linux") {
        MixlyEnv.python3Path = '/usr/bin/python3';
    } else {
        MixlyEnv.python3Path = MixlyEnv.clientPath + '/mixpyBuild/win_python3/python3.exe';
    }
    MixlyEnv.indexPath = resolve(__dirname);
    if (MixlyEnv.currentPlatform == "win32") {
        try {
            MixlyEnv.indexPath = MixlyEnv.indexPath.replace(/\\/g, "/");
        } catch (e) {
            console.log(e);
        }
    }

    if ('localStorage' in window && window['localStorage'] != null && window.localStorage["winSize"]) {
        MixlyEnv.winSize = window.localStorage["winSize"];
        let winSize = parseInt(MixlyEnv.winSize);
        if (winSize !== NaN && winSize >= 50 && winSize <= 150) {
            electronwebContents.setZoomFactor(winSize / 100);
        } else {
            MixlyEnv.winSize = 100;
            electronwebContents.setZoomFactor(1);
            window.localStorage["winSize"] = 100;
        }
    }
    console.log(nowWindow.getBackgroundColor());
    if ('localStorage' in window && window['localStorage'] != null && window.localStorage["Theme"]) {
        let winTheme = window.localStorage["Theme"];
        if (winTheme === "Dark") {
            nowWindow.setBackgroundColor("#181818");
        } else if (winTheme === "Light") {
            nowWindow.setBackgroundColor("#fff");
        }
    }
}
